define(["../../js/request.js"], function(requestHub) {
	var scope = this,
		$$ = bowlder,
	  	widget = scope.$widget,
	  	roles = widget.roles;

	var totalCounter = 0;
	var picFileList = [];
	var postDoneLastTime = false;
	var sizeLimit = 2 * 1024 * 1024;
	var uploadNum,
		totalNum;
	var createPreviewNode = function(obj) {
		var li = document.createElement('li'),
			span = document.createElement('span'),
			img = document.createElement('img'),
			deleteIcon = document.createElement('span'),
			status = document.createElement('span');
		span.innerText = obj.name;
		var sizeLimitTip = document.createElement('span');
		sizeLimitTip.innerText = '(尺寸超过限制)';
		sizeLimitTip.setAttribute('class', 'size_limit');
		deleteIcon.setAttribute('class', 'icon_delete');
		img.src = obj.src;
		li.appendChild(img);
		li.appendChild(span);
		obj.size > sizeLimit && li.appendChild(sizeLimitTip);
		li.appendChild(status);
		li.appendChild(deleteIcon);
		li.setAttribute('class', 'pic_item');
		return li;
	};

	var postPic = function(pic, i) {
		var formData = new FormData();
		formData.append('file', pic);
		// formData.append('uploadtype', 'nos');
		// formData.append('bucketName', 'cms-bucket');
		// formData.append('from', 'lhzhou');
		// formData.append('watermark', 'hello');
		var xhr = new XMLHttpRequest();
	//    xhr.open("POST", 'https://newcms.ws.netease.com/upload/nos/water');
	    xhr.open("POST", 'http://test.pic.ws.netease.com/photo/upload.gate');
	    xhr.onreadystatechange = function () {
	        if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
	        	console.log(JSON.parse(xhr.responseText));
	        	totalCounter--;
	        	var picList = document.getElementById('picList');
	        	var thisNode = picList.children[i];
	        	thisNode.children[2].setAttribute('class', 'status_done');
	        	thisNode.children[2].innerText = '上传成功';
	        	uploadNum.innerText = picFileList.length - totalCounter;
	        	if (totalCounter === 0) {
	        		console.log('全部图片上传成功，共' + picFileList.length + '张');
	        		postDoneLastTime = true;
	        	}
	        }
	    };
	    xhr.send(formData);
	};

	scope.init = function(widget) {
		var selectFile = document.getElementById('selectFile');
		var selectSkin = document.getElementById('selectSkin');
		var picList = document.getElementById('picList');
		var postPicList = document.getElementById('postPicList');
		uploadNum = document.getElementById('uploaded');
		totalNum = document.getElementById('total');

		selectSkin.addEventListener('click', function() {
			if (postDoneLastTime) {
				//上次上传操作已完成,清空缓存数组
				picFileList = [];
				picList.innerHTML = '';
				totalNum.innerText = 0;
				uploadNum.innerText = 0;
				postDoneLastTime = false;
			}
			selectFile.click();
		}, false);
		selectFile.addEventListener('change', function(e) {
			var fileList = e.target.files;
			console.log(fileList);
			for (var i=0; i<fileList.length; i++) {
				var f = fileList[i];
				var reader = new FileReader();
				reader.onload = (function(theFile) {
					return function(e) {
						var liNode = createPreviewNode({
							name: theFile.name,
							src: e.target.result,
							size: theFile.size
						});
						picList.appendChild(liNode);
						theFile.size <= sizeLimit && picFileList.push(theFile);
						totalNum.innerText = picFileList.length;
					}
				})(f);
				reader.readAsDataURL(f);
			}
		}, false);

		postPicList.addEventListener('click', function() {
			console.log(picFileList);
			if (totalCounter !== 0) {
				alert('有图片正在上传中，请稍后再试');
				return;
			}
			if (postDoneLastTime) {
				// picFileList = [];
				// picList.innerHTML = '';
				alert('本次上传已成功，请再次选择图片上传');
				return;
			}
			totalCounter = picFileList.length;
			for(var i=0;i<picFileList.length;i++) {
				var pic = picFileList[i];
				postPic(pic, i);
			}
		}, false);
	};

});